package org.esti.controller;

import org.esti.model.Interview;
import org.esti.model.User;
import org.esti.service.InterviewService;
import org.esti.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Artem Loginov (logart)
 */

/**
 * This controller is used for providing mapping between request and view
 * and showing different interview information.
 * For example: is this interview passed or not or date when it happens.
 */

@Controller
public class InterviewController {

    @Autowired
    private UserService userService;

    @Autowired
    private InterviewService interviewService;

    /**
     * Show user passed interviews.
     * This method generate list off all interviews user ever passed on this site
     * and put this list in to model
     *
     * @param login of user whose interview we want to retrieve
     * @param model set of parameters which will be passed to view. In this case all user interviews
     * @return name of view which will be render
     */
    @RequestMapping(value = "/{login}/interviews")
    public String showPastInterviews(@PathVariable String login, Model model) {
        User user = userService.getUserByLogin(login);

        model.addAttribute("user", user);

        List<Interview> interviews = interviewService.getUserInterviews(user.getId());
        model.addAttribute("interviews", interviews);

        return "interviews";
    }

    /**
     * Shows single interview by it's identity in the database
     *
     * @param id    of interview we want to see
     * @param model set of parameters which will be passed to view. In this case single interview
     * @return name of view which will be render
     */
    @RequestMapping(value = "/interview/{id}")
    public String showInterview(@PathVariable long id, Model model) {

        model.addAttribute("interview", interviewService.getInterview(id));

        model.addAttribute("interviewResults", interviewService.getInterviewResults(id));


        return "interview";
    }

}
